<?php
namespace app\home\model\page\share;

use app\api\model\dao\Goods;
use app\api\model\dao\Note;
use app\library\CommonConfig;
use app\library\Error;
use app\library\PageBase;

class GetShareInfo extends PageBase
{

    public function __construct()
    {
        parent::__construct();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            //return $this->getRes(Error::ERR_NO_AUTH);
        }

        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        if (1 == $param['type']) {
            //商品
            $obj = new Goods();
            $conds = [
                'goods_id' => $param['id'],
            ];
            $fileds = ['goods_name', 'goods_thumb'];
            $goodsInfo = $obj->getGoodsByCond($conds, $fileds);
//            var_dump($goodsInfo);
            if (empty($goodsInfo)) {
                return $this->getRes(Error::ERROR_GOODS_NOT_EXIST);
            }
            if (false == $goodsInfo) {
                return $this->getRes(Error::ERR_SYS);
            }
            $data = [
                'title' => $goodsInfo[0]['goods_name'],
                'content' => '这是商品的详情介绍文字',
                //'picture' => $goodsInfo[0]['goods_thumb'],
                'url' => 'http://139.129.232.119:8081/api/share/goodsinfo?' . 'goods_id=' . $param['id'],
            ];
            if (empty($goodsInfo[0]['goods_thumb'])) {
                $data['picture'] = CommonConfig::LOGO;
            } else {
                $data['picture'] = $goodsInfo[0]['goods_thumb'];
            }
            $this->data = $data;
            return $this->getRes();
        }
        if (2 == $param['type']) {
            //帖子
            $obj = new Note();
            $noteInfo = $obj->getNoteByid($param['id'], ['title', 'content', 'pic']);
            if (empty($noteInfo)) {
                return $this->getRes(Error::ERR_NOTE_NOT_EXIST);
            }
            if (false == $noteInfo) {
                return $this->getRes(Error::ERR_SYS);
            }
            //print_r($noteInfo);

            $data = [
                'title' => $noteInfo['title'],
                'content' => $noteInfo['title'],
                //'picture' => explode(',', urldecode($noteInfo['pic']))[0],
                'url' => 'http://139.129.232.119:8081/api/share/noteinfo?' . 'note_id=' . $param['id'],
            ];
            if (empty($noteInfo['pic'])) {
                $data['picture'] = CommonConfig::LOGO;
            } else {
                $data['picture'] = explode(',', urldecode($noteInfo['pic']))[0];
            }
            $this->data = $data;
            return $this->getRes();
        }

    }

    public function checkParam($param)
    {
        $rule = [
            'id' => 'require',
            'type' => 'require|in:1,2',
        ];
        $msg = [
            'type.require' => 'type必须',
            'type.in' => '类型错误',
            'id' => 'id必填',
        ];
        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }
}